package com.jnchen.jwt;

import com.jnchen.jwt.api.JwtInterface;
import com.jnchen.jwt.entity.User;
import com.jnchen.jwt.impl.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 验证主类
 *
 * @author jnchen
 * @date 2020/10/30 11:28
 */
@SuppressWarnings({"all"})
public class Main {


    public void assertSuccess() {

        JwtInterface auth0 = new Auth0JwtImpl();
        JwtInterface fusionAuth = new FusionAuthImpl();
        JwtInterface jjwt = new JjwtImpl();
        JwtInterface jose4j = new Jose4jImpl();
        JwtInterface nimbusJose = new NimbusJoseJwtImpl();
        JwtInterface vertx = new VertxImpl();

        User user = User.randomUser();

        String token = auth0.createJwt(user, 1000 * 60 * 5);
        assert auth0.checkJwt(token);
        User parsedUser = auth0.parseJwt(token);
        assert parsedUser.equals(user);

        token = fusionAuth.createJwt(user, 1000 * 60 * 3);
        assert fusionAuth.checkJwt(token);
        parsedUser = fusionAuth.parseJwt(token);
        assert parsedUser.equals(user);

        token = jjwt.createJwt(user, 1000 * 60 * 3);
        assert jjwt.checkJwt(token);
        parsedUser = jjwt.parseJwt(token);
        assert parsedUser.equals(user);

        token = jose4j.createJwt(user, 1000 * 60 * 3);
        assert jose4j.checkJwt(token);
        parsedUser = jose4j.parseJwt(token);
        assert parsedUser.equals(user);

        token = nimbusJose.createJwt(user, 1000 * 60 * 3);
        assert nimbusJose.checkJwt(token);
        parsedUser = nimbusJose.parseJwt(token);
        assert parsedUser.equals(user);

        token = vertx.createJwt(user, 1000 * 60 * 3);
        assert vertx.checkJwt(token);
        parsedUser = vertx.parseJwt(token);
        assert parsedUser.equals(user);

    }

    public void evaluateEfficiency() {
        List<JwtInterface> jwtImpls = new ArrayList<>();

        jwtImpls.add(new Auth0JwtImpl());
        jwtImpls.add(new FusionAuthImpl());
        jwtImpls.add(new JjwtImpl());
        jwtImpls.add(new Jose4jImpl());
        jwtImpls.add(new NimbusJoseJwtImpl());
        jwtImpls.add(new VertxImpl());

        long[] encodeTimes = new long[jwtImpls.size()];
        long[] decodeTimes = new long[jwtImpls.size()];

        int[] len = {10, 10000, 1000000};

        for (int value : len) {
            System.out.println("测试次数:" + value);

            for (int j = 0; j < value; j++) {
                User user = User.randomUser();
                for (int k = 0; k < jwtImpls.size(); k++) {
                    JwtInterface jwt = jwtImpls.get(k);
                    long time0 = System.currentTimeMillis();
                    String token = jwt.createJwt(user, 1000 * 60);
                    long time1 = System.currentTimeMillis();
                    User parsedUser = jwt.parseJwt(token);
                    long time2 = System.currentTimeMillis();
                    encodeTimes[k] += (time1 - time0);
                    decodeTimes[k] += (time2 - time1);
                }
            }
            for (int j = 0; j < jwtImpls.size(); j++) {
                System.out.println(jwtImpls.get(j).getClass().getName() + "加密总用时:" + encodeTimes[j] + "ms, 解密总用时:" + decodeTimes[j] + "ms");
            }
            Arrays.fill(encodeTimes, 0);
            Arrays.fill(decodeTimes, 0);
        }

    }

    public static void main(String[] args) {
        new Main().evaluateEfficiency();
    }

}
